import moment from 'moment';
import request from "./request";
import { filterUndefindObj } from '@src/utils/dataHandlingFunc';

// 获取标项
export const getBidding = async () => {
  const res = await request({
    url: '/resources/tender',
    method: 'get'
  });

  return res;
};

// 新建标项
export const createBidding = async ({
  title,
  description,
  expiration_date,
  source,
  destination,
  marketing,
  bandwidth,
  contract,
  business_id,
  attachments,
  min_budget,
  max_budget,
  unit
}) => {

  const newParams = filterUndefindObj({
    title,
    description,
    expiration_date: moment(expiration_date).format('YYYY-MM-DD HH:mm:ss'),
    source,
    destination,
    marketing,
    bandwidth,
    contract,
    business_id,
    min_budget,
    max_budget,
    unit,
    attachments
  });

  const res = await request({
    url: '/resources/tender',
    method: 'post',
    data: newParams
  });


  return res;
};

// 获取所有洲
export const getAllContinent = async () => {
  const res = await request({
    url: '/public/continent',
    method: 'get',
  });

  return res;
};

// 获取所有国家
export const getAllCountry = async () => {
  const res = await request({
    url: '/public/country',
    method: 'get',
  });

  return res;
};

// 获取所有城市
export const getAllCity = async () => {
  const res = await request({
    url: '/public/city',
    method: 'get'
  });

  return res;
};

// 我的投标
export const getMyBidding = async () => {
  const res = await request({
    url: '/resources/bidding',
    method: 'get'
  });

  return res;
};

// 邀请投标
export const inviteBiddingApi = async ({
  tender_id,
  user
}) => {
  const res = await request({
    url: '/resources/invite/bidding',
    method: 'post',
    headers: {
      'Content-Type': 'application/json'
    },
    propsType: 'custom',
    data: {
      tender_id,
      user_list: user
    }
  });

  return res;
};

// 获取用户
export const getUserListApi = async () => {
  const res = await request({
    url: '/user/',
    method: 'get'
  });

  return res;
};

// 现在投标
export const biddingNowApi = async ({
  tender_id,
  budget,
  delivery_time,
  description
}) => {
  const res = await request({
    url: '/resources/bidding',
    method: 'post',
    data: {
      tender_id,
      budget,
      delivery_time,
      description
    }
  });

  return res;
};

// 拒绝投标
export const biddingRejectApi = async ({
  tender_id
}) => {
  const res = await request({
    url: `/resources/reject/bidding?tender_id=${tender_id}`,
    method: 'get'
  });

  return res;
};
